package com.cc.openeye.dynamic.follow

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import com.cc.openeye.dynamic.follow.entity.followResponse.FollowItemStates
import com.cc.openeye.dynamic.follow.entity.followResponse.FollowItemType
import com.cc.openeye.dynamic.follow.entity.followResponse.Item
import com.cc.openeye.dynamic.follow.item.follow.FollowItemComponent
import com.cc.openeye.dynamic.follow.item.follow.FollowItemComponentCallBack
import com.tencent.kuikly.compose.foundation.layout.Box
import com.tencent.kuikly.compose.foundation.layout.fillMaxHeight
import com.tencent.kuikly.compose.foundation.layout.fillMaxWidth
import com.tencent.kuikly.compose.foundation.layout.padding
import com.tencent.kuikly.compose.foundation.lazy.LazyColumn
import com.tencent.kuikly.compose.foundation.lazy.itemsIndexed
import com.tencent.kuikly.compose.foundation.lazy.rememberLazyListState
import com.tencent.kuikly.compose.ui.Modifier
import com.tencent.kuikly.compose.ui.unit.dp
import com.tencent.kuikly.core.nvi.serialization.json.JSONObject
import com.tencent.kuikly.core.views.PlayState

@Composable
fun FollowComponent() {

    val followViewModel = remember {
        FollowViewModel.newInstance()
    }

    val rememberLazyListState = rememberLazyListState()

    LaunchedEffect(true) {
        followViewModel.requestFocusData()
    }

    LaunchedEffect(
        rememberLazyListState.firstVisibleItemIndex,
        rememberLazyListState.firstVisibleItemScrollOffset,
        rememberLazyListState.isScrollInProgress
    ) {
        followViewModel.onScroll(rememberLazyListState)
    }

    Box(modifier = Modifier.fillMaxWidth().fillMaxHeight()) {
        LazyColumn(
            state = rememberLazyListState,
            modifier = Modifier
                .padding(start = 15.dp, end = 15.dp, top = 15.dp)
                .fillMaxWidth()
                .fillMaxHeight(),
        ) {
            itemsIndexed(followViewModel.datas) { index, item ->

                when (item.itemType) {
                    FollowItemType.Video -> {
                        FollowItemComponent(
                            item as Item,
                            followViewModel.followItemStatesList[index],
                            callBack = object : FollowItemComponentCallBack {
                                override fun onPlayStateChange(
                                    followItemStates: FollowItemStates,
                                    state: PlayState,
                                    extInfo: JSONObject
                                ) {
                                    followViewModel.changePlayState(
                                        followItemStates,
                                        state,
                                        extInfo
                                    )
                                }
                            }
                        )
                    }

                    FollowItemType.UserInfo -> {

                    }

                    FollowItemType.LoadMore -> {

                    }
                }
            }
        }
    }
}
